#include "windowNewUser.h"
#include "windowInit.h"
#include "windowRooms.h"
#include "singleSocket.h"
#include <jsoncpp/json.h>
#include <string>
#include "user.h"

using std::string;
using std::vector;

windowNewUser::windowNewUser(sdlScreen& screen): windowLogin::windowLogin(screen){
	this->imgPass2.load("imagenPass2.gif");
	this->botInputP2.load("botonInput.gif");
	this->entradaP2.setFilter(asterisk);
	this->writingPass2 = false;
}

void windowNewUser::show(){
	windowLogin::show();
	int	actHeight = 20 + this->imgUser.height() + 25 + 100;
	actHeight += this->imgPass.height() + 25; 
	int actWidth = (screen.width() - imgPass2.width())/2;
	actHeight += 100;
	this->screen.applySurface(actWidth, actHeight, this->imgPass2);
	actWidth = (screen.width() - botInputP2.width())/2;
	actHeight += this->imgPass2.height() + 25; 
	this->screen.applySurface(actWidth, actHeight, this->botInputP2);
	this->botInputP2.setPosition(actWidth,actHeight);
	int x,y;
	this->botInputP2.getPosition(x,y);
	x += (this->botInputP2.width() - this->entradaP2.width())/2;
	y += (this->botInputP2.height() - this->entradaP2.height())/2;
	this->screen.applySurface(x,y,this->entradaP2);
	this->screen.refresh();

}

bool windowNewUser::handleEvent(const SDL_Event& event, window*& win){
	win = NULL;
	bool b = false; //Vale true si la venta entendió el evento
	vector<sdlStringInput*> vInputs;
	vInputs.push_back(&(this->entradaU));
	vInputs.push_back(&(this->entradaP));
	vInputs.push_back(&(this->entradaP2));
	vector<bool*> vBools;
	vBools.push_back(&(this->writingUser));
	vBools.push_back(&(this->writingPass));
	vBools.push_back(&(this->writingPass2));
	if (event.type == SDL_MOUSEBUTTONDOWN){
		if (event.button.button == SDL_BUTTON_LEFT){
			if (this->botBack.wasClicked(event)){
				this->sndBotClick.play();
				win = this->handleBotBack();
				SDL_Delay(150); // Vale la espera?
				b = true;
			} else if (this->botInputU.wasClicked(event)){
				this->handleBotInput(vInputs, vBools, 0);
				b = true;
			} else if (this->botInputP.wasClicked(event)){
				this->handleBotInput(vInputs, vBools, 1);
				b = true;
			} else if (this->botInputP2.wasClicked(event)){
				this->handleBotInput(vInputs, vBools, 2);
				b = true;
			} else if (this->botAccept.wasClicked(event)){
				this->sndBotClick.play();
				//Posibles tipos de errores, usuario existente bla bla
				string path("errorUserPass.gif");
				win = this->handleBotAccept(vInputs,vBools,path);
				b = true;
				SDL_Delay(150); // Vale la espera?
			}
		}
	} else if (event.type == SDL_KEYDOWN){
		if (event.key.keysym.sym == SDLK_RETURN || event.key.keysym.sym == SDLK_KP_ENTER){
			win = this->handleReturn(vInputs,vBools);
			b = true;
		} else if (event.key.keysym.sym == SDLK_TAB){
			this->handleTab(vInputs,vBools);
			b =true;
		} else if (this->writingUser){
			b = this->entradaU.handleEvent(event);
			this->show();
		} else if (this->writingPass){
			b = this->entradaP.handleEvent(event);
			this->show();
		} else if (this->writingPass2){
			b = this->entradaP2.handleEvent(event);
			this->show();
		}
	}
	return b;
}

bool windowNewUser::procesarDatos(){
	if(this->entradaP.getTexto() != entradaP2.getTexto()){return false;}
	Json::Value root;
	root["Opcion"] = "Crear cuenta";
	root["Nombre de usuario"] = this->entradaU.getTexto();
	singleSocket* socket = singleSocket::getInstance();
	if (!socket->conectado()){socket->conectar();}
	socket->enviar(root);

	Json::Value respuesta;
	socket->recibir(respuesta);
	string disponibilidad = respuesta.get("Respuesta","").asString();

	if(disponibilidad == "Usuario disponible"){
		Json::Value rootContrasenia;
		rootContrasenia["Contrasenia"] = this->entradaP.getTexto();
	
		socket->enviar(rootContrasenia);
		User* user = User::getInstance();
		user->setName(this->entradaU.getTexto());
		user->setPass(this->entradaP.getTexto());
		return true;
	} else{
		return false;
	}
}

windowNewUser::~windowNewUser(){}
